package com.uottawa.library.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uottawa.library.domain.Item;
import com.uottawa.library.domain.User;

import com.uottawa.library.domain.Loan;

import com.uottawa.library.service.LoanService;

/**
 * Servlet implementation class LoanServlet
 */

@WebServlet(name="LoanServlet",  urlPatterns={"/borrowItem.do", "/displayLoan.do", "/returnItem.do","/renewLoan.do" ,"/showUserLoan.do"})


public class LoanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private LoanService loanService = new LoanService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String url = request.getServletPath();

		if(url.contains("borrow")) {
    		String id = request.getParameter("id");
    		if(id != null) {
    			User user = (User) request.getSession().getAttribute("user");
    			Item item = new Item();
    			item.setItemId(Integer.parseInt(id));
    			String result = loanService.borrowLoanCopy(item, user);
    			request.setAttribute("result", result);			
    		}
    	}
		

		if(url.contains("display"))
		{
			List<Loan> loans = loanService.getAllLoan();
			request.setAttribute("loans", loans);
			
		}
		else if (url.contains("showUserLoan"))
		{
			User user = (User) request.getSession().getAttribute("user");
			List<Loan> loans = loanService.getAllUserLoan(user.getUserId());
			request.setAttribute("loans", loans);

			
		}
		
		else if (url.contains("renew"))
		{
			String loanId = request.getParameter("loanId");
			User user = (User) request.getSession().getAttribute("user");
			
			if(loanId !=null)
			{
			String renewOperationResponse = loanService.renewLoan(Integer.parseInt(loanId), user);
			request.setAttribute("result", renewOperationResponse);
			}

			List<Loan> loans = loanService.getAllUserExtendableLoan(user.getUserId());
			request.setAttribute("loans", loans);
			
		}

		if(url.contains("return")) {
			User user = (User) request.getSession().getAttribute("user");
			String loanId = request.getParameter("loanId");
			Loan loan = new Loan();
			loan.setLoanId(Integer.parseInt(loanId));
			loan.setUserId(user.getUserId());
			String returnOperation = loanService.returnLoan(loan);
			request.setAttribute("result", returnOperation);
		}
		
    	request.getRequestDispatcher("/loan.jsp").forward(request, response);

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    	}
	}


